package l1j.opqlo.Timer.Controler;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collection;
import java.util.Iterator;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.opqlo.Server.L1World.L1World;
import l1j.opqlo.Timer.Controler.Server.ServerWarExecutor;
import l1j.server.Config;
import l1j.server.GameServer.GameServer;
import l1j.server.Thread.PcOtherThreadPool;
import l1j.server.server.model.L1Clan;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_SkillSound;
import l1j.server.server.serverpackets.S_Sound;
import static l1j.server.server.model.skill.L1SkillId.狂暴打擊;
import static l1j.server.server.model.skill.L1SkillId.鋼鐵防禦;
import static l1j.server.server.model.skill.L1SkillId.賞金獵人;
import static l1j.server.server.model.skill.L1SkillId.狩獵專長;
import static l1j.server.server.model.skill.L1SkillId.ATTRACT_MONSTER;
/**
 * opqlo雜務 時間軸<BR>
 * 解卡點封包 發送處理<BR>
 *
 *
 */
public class 雜務計時器 extends TimerTask {

	private static final Logger _log = Logger.getLogger(雜務計時器.class.getName());

	/**
	 * 主判斷
	 *
	 * @param tgpc
	 * @return true:執行 false:不執行
	 */
	private static boolean check(L1PcInstance tgpc) {
		try {
			if (tgpc == null) {
				return false;
			}

			if (tgpc.getOnlineStatus() == 0) {
				return false;
			}

			if (tgpc.getNetConnection() == null) {
				return false;
			}

			if (tgpc.isTeleport()) {
				return false;
			}

			if (tgpc.isDead()) {// 死亡
				return false;
			}

			if (tgpc.getCurrentHp() <= 0) {// 目前HP小於0
				return false;
			}

			switch (tgpc.getMapId()) {
			case 340:// 古鲁丁商店村
			case 350:// 奇岩商店村
			case 360:// 欧瑞商店村
			case 370:// 银骑士商店村
			case 1784:
				return false;
			}
		} catch (final Exception e) {
			return false;
		}
		return true;
	}
	
	

	private ScheduledFuture<?> _timer;

	private byte _count;

	@Override
	public void run() {
//		checkNet();
		try {
			if (this._count == 5) {
			final Collection<L1PcInstance> all = L1World.getInstance().getAllPlayers();
			// 不包含元素
			if (all.isEmpty()) {
				return;
			}
			L1PcInstance pc;
			for (final Iterator<L1PcInstance> iter = all.iterator(); iter.hasNext();) {
				pc = iter.next();
				if (check(pc)) {
					// tgpc.sendPackets(new
					// S_Paralysis(S_Paralysis.TYPE_TELEPORT_UNLOCK, true));
					// Thread.sleep(1);
					if(pc.hasSkillEffect(ATTRACT_MONSTER)){
						pc.sendPacketsX8(new S_Sound(165));
						pc.sendPacketsX8(new S_SkillSound(pc.getId(),4627));
					}
						if (pc.hasSkillEffect(狂暴打擊)) {
							if(checkWar(pc)){
								pc.sendPacketsX8(new S_SkillSound(pc.getId(),418));
							}else{
								pc.removeSkillEffect(狂暴打擊);
							}
						} else if (pc.hasSkillEffect(鋼鐵防禦)) {
							if(checkWar(pc)){
								pc.sendPacketsX8(new S_SkillSound(pc.getId(),419));
							}else{
								pc.removeSkillEffect(鋼鐵防禦);
							}
						} else if (pc.hasSkillEffect(賞金獵人)) {
							if(checkWar(pc)){
								pc.sendPacketsX8(new S_SkillSound(pc.getId(),5763));
							}else{
								pc.removeSkillEffect(賞金獵人);
							}
						} else if (pc.hasSkillEffect(狩獵專長)) {
							if(checkWar(pc)){
								pc.sendPacketsX8(new S_SkillSound(pc.getId(),424));
							}else{
								pc.removeSkillEffect(狩獵專長);
							}
						}
					}
				}
			}
			if (this._count == 5) {
				this._count = 0;
			}
			// System.out.println(_count);
			this._count++;

		} catch (final Exception e) {
			_log.log(Level.SEVERE, "Lawful更新處理時間軸異常重啟", e);
			PcOtherThreadPool.get().cancel(this._timer, false);
			final 雜務計時器 lawfulTimer = new 雜務計時器();
			lawfulTimer.start();
		}
	}

	public void start() {
		final int timeMillis = 3000;
		this._timer = PcOtherThreadPool.get().scheduleAtFixedRate(this,timeMillis, timeMillis);
	}
	
	private static boolean checkWar(final L1PcInstance pc){
		final L1Clan clan = pc.getClan();
		if(clan !=null){
			final int castle_id = clan.getCastleId();
			if(castle_id >0){
				if(!ServerWarExecutor.get().isNowWar(castle_id)){
					return true;
				}
			}
		}
		return false;
	}
	
	
	private void checkNet(){
		if(Config.UPDATE_LIGHT == false){
			return;
		}
		try {
			final URL myURL = new URL("http://tw.yahoo.com");
			final HttpURLConnection myURLConnection = (HttpURLConnection) myURL.openConnection();
			myURLConnection.setReadTimeout(1000);
			myURLConnection.connect();
			if(myURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK){
				if(GameServer.LIGHT_IS_OPEN == false){
					GameServer.getInstance().UpdateLight(true);
				}
			}
			myURLConnection.disconnect();
		}catch (MalformedURLException e) {
			System.out.println("偵測到網路異常。");
			if(GameServer.LIGHT_IS_OPEN){
				GameServer.getInstance().UpdateLight(false);
			}
		} catch (IOException e) {
			System.out.println("偵測到網路異常。");
			if(GameServer.LIGHT_IS_OPEN){
				GameServer.getInstance().UpdateLight(false);
			}
		}catch(Exception e){
		}
	}
	
}
